import os
from django.newforms import fields
from settings import PROJECT_ROOT

class ProvField(fields.ChoiceField):
    def __init__(self, nation='it', required=True, widget=None, label=None, initial=None, help_text=None):
        self.__nation = nation
        choices = [("", "- Scegli"),] + self.getProvFromFile()
        super(ProvField, self).__init__(choices, required, widget, label, initial, help_text)
        
    def getProvFromFile(self):
        try:
            filepath = os.path.join(os.getcwd(), PROJECT_ROOT, "libs", "extensions", "ext")
            file = open('%s/prov.%s' % (filepath, self.__nation), 'r')
            return [(p, p) for p in file.read().split()]
            file.close()
        except:
            return [('','---'),] 
